angular.module("driver").controller("OrderCtrl", ['$scope', '$meteor', '$rootScope', '$state', '$ionicPopup', 'Map',
  function($scope, $meteor, $rootScope, $state, $ionicPopup, Map) {

    $meteor.subscribe('orders').then(function(subscriptionHandle) {
      $scope.orders = $meteor.collection(Orders);
    });

    $scope.request = function () {
      console.log(Meteor.user());
      if (!Meteor.user()) {
            //throw new Meteor.Error("未登录！", "必须先登录！");           
        var alertPopup = $ionicPopup.alert({
          title: '温馨提示',
          template: '请您先登录！',
          okType: 'button-royal',
          okText: '确定'
        });
        alertPopup.then(function(res) {
          $state.go('account');
        });
        return false;
      }

      $meteor.call('addRequest').then(
        function (data) {
          $meteor.call('setRequest', Meteor.userId(), true);
          if ($state.current.name !== 'main.amap')
            $state.go('main.amap');
          //成功发送申请，不做处理，页面上面自动有派发订单的提示。
          //console.log('data = ', data);
        },
        function (err) {
          var alertPopup = $ionicPopup.alert({
            title: err.error,
            template: '<i>' + err.reason + '</i>',
            okType: 'button-royal',
            okText: '确定'
          });
        }
      );
    };
    
    $scope.pay = function (order) {
      if (order.state == 'complete')
        return;
/*
      $meteor.call('pay', order._id, 1, 'alipay', '支付代驾费用').then(
        function(charge) {
          console.log(charge);
          pingpp.createPayment(charge, function(result, error){
	         console.log(result, error);
	          if (result == "success") {
	              // 只有微信公众账号 wx_pub 支付成功的结果会在这里返回，其他的 wap 支付结果都是在 extra 中对应的 URL 跳转。
	          } else if (result == "fail") {
	              // charge 不正确或者微信公众账号支付失败时会在此处返回
	          } else if (result == "cancel") {
	              // 微信公众账号支付取消支付
	          }
          });

        },
        function(err) {
          console.log('failed', err);
        }
      );
*/      
    };

    /*$scope.onHold = function () {
 
      var confirmPopup = $ionicPopup.confirm({
     title: '温馨提示',
     template: '正在为您联络代驾司机，确定现在要取消订单？',
     cancelText: '继续等待', 
     cancelType: 'button-royal button-outline',
     okText: '取消订单', 
     okType: 'button-assertive button-outline',
   });
   confirmPopup.then(function(res) {
     if(res) {
      $state.go('main.amap');
     } else {
       //console.log('继续等待');
     }
   });

      
    };*/

  }
]);
